﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl/OPenSsl_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl/OPenSsl_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_OPenssl.cpp -o TestFor_OPenssl -L ../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib -L ../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl -lNetEngine_BaseLib -lNetEngine_OPenSsl -Wl,-rpath=../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib:../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl


void md5cal()
{
    int nLen;
    UCHAR tszMD5[20];
    memset(tszMD5, '\0', 20);
    LPCTSTR lpszFile = _T("./NetEngine_OPenSsl.dll");

    if (!OPenSsl_Api_Digest(lpszFile, tszMD5, &nLen, TRUE, NETENGINE_OPENSSL_API_DIGEST_MD5))
    {
        return ;
    }

    for (int i = 0; i < 16; i++)
    {
        printf(_T("%02X"), tszMD5[i]);
    }
    printf(_T("\n"));
}
void Cryptto()
{
    UCHAR tszSourceBuffer[4096];
    memset(tszSourceBuffer, '\0', sizeof(tszSourceBuffer));
    const char * lpszFileName = "./NetEngine_MQService.dat";
    FILE *pSt_File = NULL;
    if (NULL == (pSt_File = fopen(lpszFileName, "rb")))
    {
        return ;
    }
    int nSize = fread(tszSourceBuffer, sizeof(UCHAR), 4096, pSt_File);
    if (nSize <= 0)
    {
        return;
    }

    LPCTSTR lpszKey = _T("xyry");
    UCHAR tszOutString[1024];
    TCHAR tszDeString[1024];
    int nLen = nSize;
    memset(tszDeString, '\0', 1024);
    memset(tszOutString, '\0', 1024);

    if (!OPenSsl_Api_CryptEncodec((const char *)tszSourceBuffer, tszOutString, &nLen, lpszKey))
    {
        return;
    }
    printf(_T("加密后的数据长度：%d,数据为\n"), nLen);
    if (NULL == (pSt_File = fopen(lpszFileName, "wb")))
    {
        return;
    }
    nSize = fwrite(tszOutString, sizeof(UCHAR), nLen, pSt_File);
    fclose(pSt_File);

    memset(tszOutString, '\0', sizeof(tszOutString));
    if (NULL == (pSt_File = fopen(lpszFileName, "rb")))
    {
        return;
    }
    nSize = fread(tszOutString, sizeof(UCHAR), 1024, pSt_File);
    fclose(pSt_File);
    if (!OPenSsl_Api_CryptDecodec(tszOutString, tszDeString, &nSize, lpszKey))
    {
        return;
    }

    printf(_T("解密后的数据长度：%d,数据为：\n"), nSize);
    printf(_T("%s\n"), tszDeString);
}
void RsaSSL()
{
    LPCTSTR lpszSource = _T("Hello World");
    LPCTSTR lpszPrivateKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test.Key");
    LPCTSTR lpszPublicKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test_pub.Key");

    if (!OPenSsl_Api_RsaGenerater(lpszPublicKey, lpszPrivateKey,"123123"))
    {
        return;
    }
    int nRsaLen = _tcslen(lpszSource);
    UCHAR puszRsaEnString[1024];
    memset(puszRsaEnString, '\0', 1024);
    if (!OPenSsl_Api_RsaEnCodec(lpszPrivateKey, lpszSource, &nRsaLen, puszRsaEnString, FALSE, "123123"))
    {
        return;
    }
    printf("%s\n", puszRsaEnString);
    TCHAR tszDeString[1024];
    memset(tszDeString, '\0', sizeof(tszDeString));
    if (!OPenSsl_Api_RsaDeCodec(lpszPublicKey, puszRsaEnString, &nRsaLen, tszDeString, TRUE))
    {
        return;
    }
    printf(_T("%s\n"), tszDeString);
}
void GetCert()
{
    LPCTSTR lpszKey = _T("E:\\openssls\\Debug\\456.crt");
    OPENSSL_X509CCINFO st_X509Info;
    memset(&st_X509Info, '\0', sizeof(OPENSSL_X509CCINFO));

    if (!OPenSSL_Cert_GetCerInfomachine(lpszKey, &st_X509Info))
    {
        return;
    }
}
void SignVer()
{
    LPCTSTR lpszSource = _T("Hello World");
    LPCTSTR lpszPass = _T("12345");

    int nLen = _tcslen(lpszSource);
    TCHAR tszSource[2048];

    memset(tszSource, '\0', sizeof(tszSource));

    LPCTSTR lpszPrivateKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test.Key");
    LPCTSTR lpszPublicKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test_pub.Key");

    if (!OPenSsl_Api_RsaGenerater(lpszPublicKey, lpszPrivateKey, lpszPass))
    {
        return;
    }

    if (!OPenSSL_Cert_SignEncoder(lpszPrivateKey, lpszPass,lpszSource, tszSource,&nLen, NETENGINE_OPENSSL_API_DIGEST_MD5))
    {
        return;
    }
    if (!OPenSSL_Cert_SignVerifly(lpszPublicKey, lpszPass,tszSource,nLen, lpszSource, 11, NETENGINE_OPENSSL_API_DIGEST_MD5))
    {
        return;
    }
}
void CertVer()
{
    LPCTSTR lpszRootKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\456.crt");
    LPCTSTR lpszUserKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\456.crt");

    if (!OPenSSL_Cert_X509Verifly(lpszRootKey, lpszUserKey))
    {
        return;
    }
}
void VerSign()
{
    LPCTSTR lpszPrivateKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test.Key");
    LPCTSTR lpszPublicKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test_pub.Key");
    LPCTSTR lpszReqFile = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test.csr");
    LPCTSTR lpszCAFile = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\ca.crt");
    LPCTSTR lpszCAKey = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\ca.Key");
    LPCTSTR lpszUserFile = _T("E:\\NetEngine_Apps\\NetEngine_WINApps\\Debug\\test.crt");
    LPCTSTR lpszPass = _T("12345");

    if (!OPenSsl_Api_RsaGenerater(lpszPublicKey, lpszPrivateKey, lpszPass))
    {
        return;
    }

    OPENSSL_X509CCINL st_X509Info;
    memset(&st_X509Info, '\0', sizeof(OPENSSL_X509CCINL));

    _tcscpy(st_X509Info.tszCommonName, "www.xyry.org");
    _tcscpy(st_X509Info.tszCountryName, "china");
    _tcscpy(st_X509Info.tszProvinceName, "sichuan");
    _tcscpy(st_X509Info.tszLocalityName, "chengdu");
    _tcscpy(st_X509Info.tszOrganizationName, "xyry");
    _tcscpy(st_X509Info.tszOrgUnitName, "soft");
    _tcscpy(st_X509Info.tszEmailAddress, "486179@qq.com");

    OPenSSL_Cert_MakeCACert(lpszCAFile, lpszCAKey,"333", 2048, 120102, 60 * 60 * 24 * 10, &st_X509Info);

    OPenSSL_Cert_X509GenRequest(lpszReqFile, &st_X509Info, lpszPrivateKey, lpszPass);
    OPenSSL_Cert_X509SignVer(lpszCAFile, lpszCAKey, "333", lpszReqFile, lpszPass, lpszUserFile, 1100220011, 60 * 60 * 24 * 10, NULL);
}
int main()
{
    Cryptto();
}
